{
  "name": "Campaign Performance Issue Escalation Agent",
  "nodes": [
    {
      "parameters": {
        "updates": ["message"]
      },
      "id": "tg-trigger-16",
      "name": "Telegram Trigger",
      "type": "n8n-nodes-base.telegramTrigger",
      "typeVersion": 1.2,
      "position": [0, 0]
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "name": "user_message",
              "value": "={{ $json.message.text }}",
              "type": "string"
            },
            {
              "name": "user_name",
              "value": "={{ $json.message.from.first_name + ' ' + ($json.message.from.last_name || '') }}",
              "type": "string"
            },
            {
              "name": "chat_id",
              "value": "={{ $json.message.chat.id }}",
              "type": "string"
            }
          ]
        }
      },
      "id": "set-context-16",
      "name": "Workflow Configuration",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [220, 0]
    },
    {
      "parameters": {
        "promptType": "define",
        "text": "=User Name: {{ $json.user_name }}\nUser Message: {{ $json.user_message }}\nChat ID: {{ $json.chat_id }}",
        "options": {
          "systemMessage": "=You are a Campaign Performance Issue Escalation Assistant.\n\nYour responsibilities:\n1. Identify whether the user wants to REGISTER a new campaign performance issue or CHECK the status of an existing campaign issue.\n\n2. For new campaign issue registrations, collect:\n   - Campaign ID\n   - Issue Type (Low CTR, High CPC, Budget Overrun, Tracking Error, Low Conversions)\n   - Brief description of the issue\n\n3. Validate the campaign using the Campaign Master Tool and extract:\n   - Campaign_Name\n   - Campaign_Status\n   - Campaign_Owner_Email (used for email communication)\n\n4. If the campaign is valid and active:\n   - Generate a unique issue reference ID in the format:\n     CMP-YYYYMMDD-[last 4 digits of campaign]-HHmmss\n   - Register the issue using the Campaign Issue Register Tool\n   - Send an acknowledgement email using the Gmail Tool to Campaign_Owner_Email\n   - Clearly explain investigation workflow and expected timelines\n   - Respond using the Telegram Response Tool\n\n5. If the campaign is invalid or inactive:\n   - Politely explain the reason\n   - Do NOT register the issue\n   - Respond using the Telegram Response Tool\n\n6. For status checks:\n   - Ask for issue reference ID if missing\n   - Retrieve current status using the Campaign Issue Status Tool\n   - Explain the current investigation stage and next steps\n   - Respond using the Telegram Response Tool\n\nStrict rules:\n- Do NOT promise performance improvement.\n- Do NOT provide marketing strategy advice.\n- Campaign owner email must be retrieved from the master sheet only.\n- ALWAYS use the Telegram Response Tool."
        }
      },
      "id": "agent-16",
      "name": "Campaign Performance Issue Agent",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "typeVersion": 3,
      "position": [500, 0]
    },
    {
      "parameters": {
        "model": { "mode": "id", "value": "gpt-4o" },
        "options": { "temperature": 0.2 }
      },
      "id": "llm-16",
      "name": "OpenAI Chat Model",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "typeVersion": 1.3,
      "position": [320, 220]
    },
    {
      "parameters": {
        "sessionIdType": "customKey",
        "sessionKey": "={{ $('Workflow Configuration').item.json.chat_id }}"
      },
      "id": "memory-16",
      "name": "Simple Memory",
      "type": "@n8n/n8n-nodes-langchain.memoryBufferWindow",
      "typeVersion": 1.3,
      "position": [500, 220]
    },
    {
      "parameters": {
        "descriptionType": "manual"
      },
      "id": "campaign-master-16",
      "name": "Campaign Master Tool",
      "type": "n8n-nodes-base.googleSheetsTool",
      "typeVersion": 4.7,
      "position": [640, 260]
    },
    {
      "parameters": {
        "operation": "append",
        "descriptionType": "manual"
      },
      "id": "campaign-register-16",
      "name": "Campaign Issue Register Tool",
      "type": "n8n-nodes-base.googleSheetsTool",
      "typeVersion": 4.7,
      "position": [800, 260]
    },
    {
      "parameters": {
        "descriptionType": "manual"
      },
      "id": "campaign-status-16",
      "name": "Campaign Issue Status Tool",
      "type": "n8n-nodes-base.googleSheetsTool",
      "typeVersion": 4.7,
      "position": [960, 320]
    },
    {
      "parameters": {
        "sendTo": "={{ $fromAI('campaign_owner_email') }}",
        "subject": "={{ $fromAI('email_subject') }}",
        "message": "={{ $fromAI('email_body') }}"
      },
      "id": "gmail-16",
      "name": "Campaign Issue Notification Email",
      "type": "n8n-nodes-base.gmailTool",
      "typeVersion": 2.1,
      "position": [1120, 220]
    },
    {
      "parameters": {
        "chatId": "={{ $('Telegram Trigger').item.json.message.chat.id }}",
        "text": "={{ $json.output }}"
      },
      "id": "tg-response-16",
      "name": "Send Telegram Response",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [960, 0]
    }
  ],
  "connections": {
    "Telegram Trigger": {
      "main": [[{ "node": "Workflow Configuration", "type": "main" }]]
    },
    "Workflow Configuration": {
      "main": [[{ "node": "Campaign Performance Issue Agent", "type": "main" }]]
    },
    "Campaign Performance Issue Agent": {
      "main": [[{ "node": "Send Telegram Response", "type": "main" }]]
    }
  },
  "active": false
}
